package com.cnb.telepathytalk.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.cnb.telepathytalk.R;
import com.cnb.telepathytalk.util.LazyImageLoader;

public class ProfilesGridAdapter extends BaseAdapter {
	private ArrayList<String> list;
	private int layoutResc;
	private LayoutInflater inflater;
	private LazyImageLoader imageLoader;

	public ProfilesGridAdapter(Context context) {
		super();
		this.list = new ArrayList<String>();
		for (int i = 0; i < 8; i++) {
			list.add("");
		}
		this.layoutResc = R.layout.grid_item_profile_pic;
		this.inflater = LayoutInflater.from(context);
		this.imageLoader = new LazyImageLoader(context, 0, 100, LazyImageLoader.SET_IMAGE);
	}

	public void clear() {
		list.clear();
		for (int i = 0; i < 8; i++) {
			list.add("");
		}
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	public void addItem(int index, String data) {
		list.set(index, data);
	}

	@Override
	public int getCount() {
		return 8;
	}

	@Override
	public String getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(layoutResc, null);
			holder = new Holder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		holder.getImageView().setImageResource(R.drawable.profile_thumb);
		if ("".equals(list.get(position))) {
		} else {
			imageLoader.DisplayImage(list.get(position), holder.getImageView());
		}
		return convertView;
	}

	private class Holder {
		View baseView;
		ImageView imageView;

		public Holder(View view) {
			super();
			baseView = view;
		}

		public ImageView getImageView() {
			if (imageView == null)
				imageView = (ImageView) baseView.findViewById(R.id.imageView1);
			return imageView;
		}
	}
}